package com.fyx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fyx.annotation.Log;
import com.fyx.entity.OnlineUserPeakEntity;
import com.fyx.entity.StayUserEntity;
import com.fyx.modules.system.service.DataService;
import com.fyx.modules.system.service.DeptService;
import com.fyx.modules.system.service.UserService;
import com.fyx.service.IStayUserService;
import com.fyx.utils.PageUtil;
import com.fyx.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "峰值分析")
@RestController
@RequestMapping("/api/stayUser")
@RequiredArgsConstructor
public class StayUserController {
     @Autowired
    private IStayUserService iStayUserService;

    private final DeptService deptService;
    private final DataService dataService;
    private final UserService userService;

    @Log("查询用户留存率")
    @ApiOperation("查询用户留存率")
    @GetMapping(value="getStayUserList")
    @PreAuthorize("@el.check('getStayUser:list')")
    public ResponseEntity<Object> getStayUserList(StayUserEntity stayUserEntity, Pageable pageable){
        // 数据权限
        if (!ObjectUtils.isEmpty(stayUserEntity.getDeptId())) {
            stayUserEntity.getDeptIds().add(stayUserEntity.getDeptId());
            stayUserEntity.getDeptIds().addAll(deptService.getDeptChildren(deptService.findByPid(stayUserEntity.getDeptId())));
        }
        List<Long> dataScopes = dataService.getDeptIds(userService.findByName(SecurityUtils.getCurrentUsername()));

        if (!CollectionUtils.isEmpty(stayUserEntity.getDeptIds()) && !CollectionUtils.isEmpty(dataScopes)){
            // 取交集
            stayUserEntity.getDeptIds().retainAll(dataScopes);
        } else {
            // 否则取并集
            stayUserEntity.getDeptIds().addAll(dataScopes);
        }
        Map mapResult = iStayUserService.getStayUserList(stayUserEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @PostMapping(value="getStayUserListChannel")
    @PreAuthorize("@el.check('getStayUser:list')")
    public List<StayUserEntity> getStayUserListChannel(@Validated @RequestBody JSONObject jsonObject){
       String channel= jsonObject.getString("channel");
       Integer time=Integer.valueOf(jsonObject.getString("time")) ;

        List<StayUserEntity> mapResult = iStayUserService.getStayUserList(time,channel);
        return mapResult;
    }
}
